<?php
if (!defined('BASEPATH'))   exit('No direct script access allowed');

/**
 * 短信接口
 */
class Sms extends CT_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('CT_sms');
        $this->load->library('CT_sms_voice');
        $this->load->library('session');
        $this->load->model('send_sms_log_model');
        $this->config->load('sms', TRUE);
    }

    /**
     * 发送文本短信
     * @return boolean
     */
    public function send()
    {
        //短信类型
        $sms_type = intval($this->input->get_post('sms_type'));
        
        switch ($sms_type) {
            case 1:
                $sms_type_str = 'login';
                break;
            default:
                $sms_type_str = 'login';
                break;
        }

        $mobile = $this->input->get_post('mobile');
        $msg = $this->input->get_post('msg');
        //短信模板
        $type = $this->input->get_post('type') ? $this->input->get_post('type') : 'code';
        
        //手机号码格式检查
        if (!$this->ct_sms->set_mobile($mobile)) {
            $this->show_json_data(NULL, 'E_1001');
            return false;
        }
        $content = $this->ct_sms->set_get_msg($type, $msg);
        $code = $this->ct_sms->get_code();
        $res = $this->ct_sms->send();
        
        //如果发送成功 写入短信记录表
        $arr = array(
            'ip' => $this->input->ip_address(),
            'mobile' => $mobile,
            'content' => '验证码：'.$code,
            'sms_type' => $sms_type,
            'create_date' => date('Y-m-d H:i:s'),
            'update_date' => date('Y-m-d H:i:s'),
            'sms_status' => intval($res)
        );
        
        $this->send_sms_log_model->insert($arr);

        if ($res) {
            //把发送类型也写入session 这样方便根据不同的发送类型 进行短信安全性限制
            $expire = $this->config->item('sms')['base']['msg_expire'];
            $key = $sms_type_str.'_'.$mobile;
            $this->session->set_tempdata($key, $code, $expire);
           
            $this->show_json_data(NULL, 'E_1000');
            exit;
        } else {
            $this->show_json_data(NULL, 'E_1002');
            exit;
        }
        
    }
    
    /**
     * 发送语音短信
     * @return boolean
     */
    public function send_voice()
    {
        //短信类型
        $sms_type = intval($this->input->get_post('sms_type'));
    
        switch ($sms_type) {
            case 1:
                $sms_type_str = 'login';
                break;
            default:
                $sms_type_str = 'login';
                break;
        }
    
        $mobile = $this->input->get_post('mobile');
        $msg = $this->input->get_post('msg');
        //短信模板
        $type = $this->input->get_post('type') ? $this->input->get_post('type') : 'valicode';
    
        //手机号码格式检查
        if (!$this->ct_sms_voice->set_mobile($mobile)) {
            $this->show_json_data(NULL, 'E_1001');
            exit;
        }
        $content = $this->ct_sms_voice->set_get_msg($type, $msg);
        $res = $this->ct_sms_voice->send();
        
        //如果发送成功 写入短信记录表
        $arr = array(
            'ip' => $this->input->ip_address(),
            'mobile' => $mobile,
            'content' => $content,
            'sms_type' => $sms_type,
            'create_date' => date('Y-m-d H:i:s'),
            'update_date' => date('Y-m-d H:i:s'),
            'sms_status' => intval($res)
        );
        
        $this->send_sms_log_model->insert($arr);
    
        if ($res) {
            //把发送类型也写入session 这样方便根据不同的发送类型 进行短信安全性限制
            $code = $this->ct_sms_voice->get_code();
            $expire = $this->config->item('sms')['base']['msg_expire'];
            $key = $sms_type_str.'_'.$mobile;
            $this->session->set_tempdata($key, $code, $expire);
             
            $this->show_json_data(NULL, 'E_1000');
            exit;
        } else {
            $this->show_json_data(NULL, 'E_1002');
            exit;
        }
    
    }

}
